package com.xq;

import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.MqttPersistenceException;
import org.eclipse.paho.client.mqttv3.MqttTopic;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;

/**
 * @author jltxseo
 *         Created by junlintianxia on 2016/07/13.
 */
public class Server {
    public static final String HOST = "tcp://172.16.32.197";
    public static final String TOPIC = "toclient/124";
    public static final String TOPIC125 = "toclient/125";
    private static final String clientid = "server";

    private MqttClient client;
    private MqttTopic topic;
    private MqttTopic topic125;
    private String userName = "admin";
    private String passWord = "ps";
    private MqttMessage message;

    public Server() throws MqttException {
        // MemoryPersistence设置clientid的保存形式，默认为以内存保存
        client = new MqttClient(HOST, clientid, new MemoryPersistence());
        connect();
    }

    private void connect() {
        MqttConnectOptions options = new MqttConnectOptions();
        options.setCleanSession(false);
//        options.setUserName(userName);
//        options.setPassword(passWord.toCharArray());
        // 设置超时时间
        options.setConnectionTimeout(10);
        // 设置会话心跳时间
        options.setKeepAliveInterval(20);
        try {
            client.setCallback(new PushCallback());
            client.connect(options);
            topic = client.getTopic(TOPIC);
            topic125 = client.getTopic(TOPIC125);
        } catch (Exception e) {
            System.out.println("Exception"+e.toString());
        }
    }

    public void publish(MqttTopic topic , MqttMessage message) throws MqttPersistenceException,
            MqttException {
        MqttDeliveryToken token = topic.publish(message);
        token.waitForCompletion();
        System.out.println("message is published completely! "
                + token.isComplete());
    }

    public static void main(String[] args) {
        try {
            Server server = new Server();

            server.message = new MqttMessage();
            server.message.setQos(2);
            server.message.setRetained(true);
            server.message.setPayload("send message to 124".getBytes());
            if(server.topic != null){
                server.publish(server.topic, server.message);
            }else {
                System.out.println("server.topic==null");
            }


            server.message = new MqttMessage();
            server.message.setQos(2);
            server.message.setRetained(true);
            server.message.setPayload("send mesage to 125".getBytes());
            if(server.topic125 != null){
                server.publish(server.topic125, server.message);
            }else {
                System.out.println("server.topic125==null");
            }

            System.out.println(server.message.isRetained() + "------ratained status");
        } catch (MqttException e) {
            System.out.println("MqttException"+e.toString());
        }
    }
}
